import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class day_07_IO_ObjectOutputStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 利用序列化流将对象写入文件
        Student stu = new Student();
        stu.setName("龚汉强");
        stu.setAge(18);

        // 创建序列化流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("TxtFile\\oss.txt"));
        // 写出数据
        oos.writeObject(stu);
        // 关闭链接
        oos.close();

        // 创建反序列化流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TxtFile\\oss.txt"));

        // 读取数据
        Student stu1 = (Student) ois.readObject();

        // 打印对象
        System.out.println(stu1);

        // 释放资源
        ois.close();
    }

}

// Serializable为标记性接口
class Student implements Serializable {
    // 指定编号
    private static final long SerialVersionUID = 1L;

    private String name;
    private Integer age;
    // 瞬时变量，不会被序列化器序列化
    private transient String address;

    public Student() {
    }

    public Student(String name, Integer age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public String getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", address=" + address + "]";
    }

}